import { isExternal } from "./validate.ts";
import type { RouteRecordRaw } from "vue-router";
/**
 * @description 根据roles数组拦截路由
 * @param routes
 * @param baseUrl
 * @returns {[]}
 */
export function filterRoutes(routes: Array<RouteRecordRaw>) {
  return routes.filter((route) => {
    return route.path !== "/:pathMatch(.*)*" && !isExternal(route.path);
  });
}

export function generateMenu(
  routes: RouteRecordRaw[],
  basePath?: string
): RouteRecordRaw[] {
  const filteredRoutes = routes
    .filter((item) => {
      return item.meta && !item.meta.hidden;
    })
    .map((route) => {
      route.path = basePath ? `${basePath}/${route.path}` : route.path;
      if (route.children && route.children.length > 0) {
        route.children = generateMenu(route.children, route.path);
      }
      if (route.children && route.children.length === 1) {
        route = route.children[0];
      }
      return route;
    });
  return filteredRoutes;
}
